﻿using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using HslCommunication.BasicFramework;
using HslCommunication.Core;
using HslCommunication.Core.Address;
using HslCommunication.Core.IMessage;
using HslCommunication.Core.Net;
using HslCommunication.Reflection;

namespace HslCommunication.Profinet.Fuji
{
	/// <summary>
	/// 富士PLC的SPB协议，详细的地址信息见api文档说明，地址可以携带站号信息，例如：s=2;D100，PLC侧需要配置无BCC计算，包含0D0A结束码<br />
	/// Fuji PLC's SPB protocol. For detailed address information, see the api documentation, 
	/// The address can carry station number information, for example: s=2;D100, PLC side needs to be configured with no BCC calculation, including 0D0A end code
	/// </summary>
	/// <remarks>
	/// 其所支持的地址形式如下：
	/// <list type="table">
	///   <listheader>
	///     <term>地址名称</term>
	///     <term>地址代号</term>
	///     <term>示例</term>
	///     <term>地址进制</term>
	///     <term>字操作</term>
	///     <term>位操作</term>
	///     <term>备注</term>
	///   </listheader>
	///   <item>
	///     <term>内部继电器</term>
	///     <term>M</term>
	///     <term>M100,M200</term>
	///     <term>10</term>
	///     <term>√</term>
	///     <term>√</term>
	///     <term>读写字单位的时候，M2代表位的M32</term>
	///   </item>
	///   <item>
	///     <term>输入继电器</term>
	///     <term>X</term>
	///     <term>X10,X20</term>
	///     <term>10</term>
	///     <term>√</term>
	///     <term>√</term>
	///     <term>读取字单位的时候，X2代表位的X32</term>
	///   </item>
	///   <item>
	///     <term>输出继电器</term>
	///     <term>Y</term>
	///     <term>Y10,Y20</term>
	///     <term>10</term>
	///     <term>√</term>
	///     <term>√</term>
	///     <term>读写字单位的时候，Y2代表位的Y32</term>
	///   </item>
	///   <item>
	///     <term>锁存继电器</term>
	///     <term>L</term>
	///     <term>L100,L200</term>
	///     <term>10</term>
	///     <term>√</term>
	///     <term>√</term>
	///     <term></term>
	///   </item>
	///   <item>
	///     <term>定时器的线圈</term>
	///     <term>TC</term>
	///     <term>TC100,TC200</term>
	///     <term>10</term>
	///     <term>√</term>
	///     <term>√</term>
	///     <term></term>
	///   </item>
	///   <item>
	///     <term>定时器的当前值</term>
	///     <term>TN</term>
	///     <term>TN100,TN200</term>
	///     <term>10</term>
	///     <term>√</term>
	///     <term>×</term>
	///     <term></term>
	///   </item>
	///   <item>
	///     <term>计数器的线圈</term>
	///     <term>CC</term>
	///     <term>CC100,CC200</term>
	///     <term>10</term>
	///     <term>√</term>
	///     <term>√</term>
	///     <term></term>
	///   </item>
	///   <item>
	///     <term>计数器的当前</term>
	///     <term>CN</term>
	///     <term>CN100,CN200</term>
	///     <term>10</term>
	///     <term>√</term>
	///     <term>×</term>
	///     <term></term>
	///   </item>
	///   <item>
	///     <term>数据寄存器</term>
	///     <term>D</term>
	///     <term>D1000,D2000</term>
	///     <term>10</term>
	///     <term>√</term>
	///     <term>√</term>
	///     <term>读位的时候，D10.15代表第10个字的第15位</term>
	///   </item>
	///   <item>
	///     <term>文件寄存器</term>
	///     <term>R</term>
	///     <term>R100,R200</term>
	///     <term>10</term>
	///     <term>√</term>
	///     <term>√</term>
	///     <term>读位的时候，R10.15代表第10个字的第15位</term>
	///   </item>
	///   <item>
	///     <term>链接寄存器</term>
	///     <term>W</term>
	///     <term>W100,W200</term>
	///     <term>10</term>
	///     <term>√</term>
	///     <term>√</term>
	///     <term>读位的时候，W10.15代表第10个字的第15位</term>
	///   </item>
	/// </list>
	/// </remarks>
	// Token: 0x020000A6 RID: 166
	public class FujiSPBOverTcp : NetworkDeviceBase
	{
		/// <summary>
		/// 使用默认的构造方法实例化对象<br />
		/// Instantiate the object using the default constructor
		/// </summary>
		// Token: 0x06000C52 RID: 3154 RVA: 0x000480B1 File Offset: 0x000462B1
		public FujiSPBOverTcp()
		{
			base.WordLength = 1;
			this.LogMsgFormatBinary = false;
			base.ByteTransform = new RegularByteTransform();
		}

		/// <summary>
		/// 使用指定的ip地址和端口来实例化一个对象<br />
		/// Instantiate an object with the specified IP address and port
		/// </summary>
		/// <param name="ipAddress">设备的Ip地址</param>
		/// <param name="port">设备的端口号</param>
		// Token: 0x06000C53 RID: 3155 RVA: 0x000480DD File Offset: 0x000462DD
		public FujiSPBOverTcp(string ipAddress, int port)
		{
			base.WordLength = 1;
			this.IpAddress = ipAddress;
			this.Port = port;
			this.LogMsgFormatBinary = false;
			base.ByteTransform = new RegularByteTransform();
		}

		/// <inheritdoc />
		// Token: 0x06000C54 RID: 3156 RVA: 0x00048119 File Offset: 0x00046319
		protected override INetMessage GetNewNetMessage()
		{
			return new FujiSPBMessage();
		}

		/// <summary>
		/// PLC的站号信息<br />
		/// PLC station number information
		/// </summary>
		// Token: 0x170001E8 RID: 488
		// (get) Token: 0x06000C55 RID: 3157 RVA: 0x00048120 File Offset: 0x00046320
		// (set) Token: 0x06000C56 RID: 3158 RVA: 0x00048128 File Offset: 0x00046328
		public byte Station
		{
			get
			{
				return this.station;
			}
			set
			{
				this.station = value;
			}
		}

		/// <summary>
		/// 批量读取PLC的数据，以字为单位，支持读取X,Y,L,M,D,TN,CN,TC,CC,R,W具体的地址范围需要根据PLC型号来确认，地址可以携带站号信息，例如：s=2;D100<br />
		/// Read PLC data in batches, in units of words. Supports reading X, Y, L, M, D, TN, CN, TC, CC, R, W. 
		/// The specific address range needs to be confirmed according to the PLC model, The address can carry station number information, for example: s=2;D100
		/// </summary>
		/// <param name="address">地址信息</param>
		/// <param name="length">数据长度</param>
		/// <returns>读取结果信息</returns>
		/// <remarks>
		/// 单次读取的最大的字数为105，如果读取的字数超过这个值，请分批次读取。
		/// </remarks>
		// Token: 0x06000C57 RID: 3159 RVA: 0x00048134 File Offset: 0x00046334
		[HslMqttApi("ReadByteArray", "")]
		public override OperateResult<byte[]> Read(string address, ushort length)
		{
			OperateResult<byte[]> operateResult = FujiSPBOverTcp.BuildReadCommand(this.station, address, length);
			bool flag = !operateResult.IsSuccess;
			OperateResult<byte[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<byte[]>(operateResult);
			}
			else
			{
				OperateResult<byte[]> operateResult2 = base.ReadFromCoreServer(operateResult.Content);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = OperateResult.CreateFailedResult<byte[]>(operateResult2);
				}
				else
				{
					OperateResult<byte[]> operateResult3 = FujiSPBOverTcp.CheckResponseData(operateResult2.Content);
					bool flag3 = !operateResult3.IsSuccess;
					if (flag3)
					{
						result = OperateResult.CreateFailedResult<byte[]>(operateResult3);
					}
					else
					{
						result = OperateResult.CreateSuccessResult<byte[]>(Encoding.ASCII.GetString(operateResult3.Content.RemoveBegin(4)).ToHexBytes());
					}
				}
			}
			return result;
		}

		/// <summary>
		/// 批量写入PLC的数据，以字为单位，也就是说最少2个字节信息，支持读取X,Y,L,M,D,TN,CN,TC,CC,R具体的地址范围需要根据PLC型号来确认，地址可以携带站号信息，例如：s=2;D100<br />
		/// The data written to the PLC in batches, in units of words, that is, a minimum of 2 bytes of information. It supports reading X, Y, L, M, D, TN, CN, TC, CC, and R. 
		/// The specific address range needs to be based on PLC model to confirm, The address can carry station number information, for example: s=2;D100
		/// </summary>
		/// <param name="address">地址信息，举例，D100，R200，TN100，CN200</param>
		/// <param name="value">数据值</param>
		/// <returns>是否写入成功</returns>
		/// <remarks>
		/// 单次写入的最大的字数为103个字，如果写入的数据超过这个长度，请分批次写入
		/// </remarks>
		// Token: 0x06000C58 RID: 3160 RVA: 0x000481D8 File Offset: 0x000463D8
		[HslMqttApi("WriteByteArray", "")]
		public override OperateResult Write(string address, byte[] value)
		{
			OperateResult<byte[]> operateResult = FujiSPBOverTcp.BuildWriteByteCommand(this.station, address, value);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				OperateResult<byte[]> operateResult2 = base.ReadFromCoreServer(operateResult.Content);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = operateResult2;
				}
				else
				{
					result = FujiSPBOverTcp.CheckResponseData(operateResult2.Content);
				}
			}
			return result;
		}

		/// <summary>
		/// 批量读取PLC的Bool数据，以位为单位，支持读取X,Y,L,M,D,TN,CN,TC,CC,R,W，例如 M100, 如果是寄存器地址，可以使用D10.12来访问第10个字的12位，地址可以携带站号信息，例如：s=2;M100<br />
		/// Read PLC's Bool data in batches, in units of bits, support reading X, Y, L, M, D, TN, CN, TC, CC, R, W, such as M100, if it is a register address, 
		/// you can use D10. 12 to access the 12 bits of the 10th word, the address can carry station number information, for example: s=2;M100
		/// </summary>
		/// <param name="address">地址信息，举例：M100, D10.12</param>
		/// <param name="length">读取的bool长度信息</param>
		/// <returns>Bool[]的结果对象</returns>
		// Token: 0x06000C59 RID: 3161 RVA: 0x00048234 File Offset: 0x00046434
		[HslMqttApi("ReadBoolArray", "")]
		public override OperateResult<bool[]> ReadBool(string address, ushort length)
		{
			byte b = (byte)HslHelper.ExtractParameter(ref address, "s", (int)this.station);
			OperateResult<FujiSPBAddress> operateResult = FujiSPBAddress.ParseFrom(address);
			bool flag = !operateResult.IsSuccess;
			OperateResult<bool[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<bool[]>(operateResult);
			}
			else
			{
				bool flag2 = address.StartsWith("X") || address.StartsWith("Y") || address.StartsWith("M") || address.StartsWith("L") || address.StartsWith("TC") || address.StartsWith("CC");
				if (flag2)
				{
					bool flag3 = address.IndexOf('.') < 0;
					if (flag3)
					{
						operateResult.Content.BitIndex = (int)(operateResult.Content.Address % 16);
						operateResult.Content.Address = operateResult.Content.Address / 16;
					}
				}
				ushort length2 = (ushort)((operateResult.Content.GetBitIndex() + (int)length - 1) / 16 - operateResult.Content.GetBitIndex() / 16 + 1);
				OperateResult<byte[]> operateResult2 = FujiSPBOverTcp.BuildReadCommand(b, operateResult.Content, length2);
				bool flag4 = !operateResult2.IsSuccess;
				if (flag4)
				{
					result = OperateResult.CreateFailedResult<bool[]>(operateResult2);
				}
				else
				{
					OperateResult<byte[]> operateResult3 = base.ReadFromCoreServer(operateResult2.Content);
					bool flag5 = !operateResult3.IsSuccess;
					if (flag5)
					{
						result = OperateResult.CreateFailedResult<bool[]>(operateResult3);
					}
					else
					{
						OperateResult<byte[]> operateResult4 = FujiSPBOverTcp.CheckResponseData(operateResult3.Content);
						bool flag6 = !operateResult4.IsSuccess;
						if (flag6)
						{
							result = OperateResult.CreateFailedResult<bool[]>(operateResult4);
						}
						else
						{
							result = OperateResult.CreateSuccessResult<bool[]>(Encoding.ASCII.GetString(operateResult4.Content.RemoveBegin(4)).ToHexBytes().ToBoolArray().SelectMiddle(operateResult.Content.BitIndex, (int)length));
						}
					}
				}
			}
			return result;
		}

		/// <summary>
		/// 写入一个Bool值到一个地址里，地址可以是线圈地址，也可以是寄存器地址，例如：M100, D10.12，地址可以携带站号信息，例如：s=2;D10.12<br />
		/// Write a Bool value to an address. The address can be a coil address or a register address, for example: M100, D10.12. 
		/// The address can carry station number information, for example: s=2;D10.12
		/// </summary>
		/// <param name="address">地址信息，举例：M100, D10.12</param>
		/// <param name="value">写入的bool值</param>
		/// <returns>是否写入成功的结果对象</returns>
		// Token: 0x06000C5A RID: 3162 RVA: 0x000483F8 File Offset: 0x000465F8
		[HslMqttApi("WriteBool", "")]
		public override OperateResult Write(string address, bool value)
		{
			OperateResult<byte[]> operateResult = FujiSPBOverTcp.BuildWriteBoolCommand(this.station, address, value);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				OperateResult<byte[]> operateResult2 = base.ReadFromCoreServer(operateResult.Content);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = operateResult2;
				}
				else
				{
					result = FujiSPBOverTcp.CheckResponseData(operateResult2.Content);
				}
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Fuji.FujiSPBOverTcp.Read(System.String,System.UInt16)" />
		// Token: 0x06000C5B RID: 3163 RVA: 0x00048454 File Offset: 0x00046654
		[DebuggerStepThrough]
		public override Task<OperateResult<byte[]>> ReadAsync(string address, ushort length)
		{
			FujiSPBOverTcp.<ReadAsync>d__10 <ReadAsync>d__ = new FujiSPBOverTcp.<ReadAsync>d__10();
			<ReadAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<byte[]>>.Create();
			<ReadAsync>d__.<>4__this = this;
			<ReadAsync>d__.address = address;
			<ReadAsync>d__.length = length;
			<ReadAsync>d__.<>1__state = -1;
			<ReadAsync>d__.<>t__builder.Start<FujiSPBOverTcp.<ReadAsync>d__10>(ref <ReadAsync>d__);
			return <ReadAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Fuji.FujiSPBOverTcp.Write(System.String,System.Byte[])" />
		// Token: 0x06000C5C RID: 3164 RVA: 0x000484A8 File Offset: 0x000466A8
		[DebuggerStepThrough]
		public override Task<OperateResult> WriteAsync(string address, byte[] value)
		{
			FujiSPBOverTcp.<WriteAsync>d__11 <WriteAsync>d__ = new FujiSPBOverTcp.<WriteAsync>d__11();
			<WriteAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<WriteAsync>d__.<>4__this = this;
			<WriteAsync>d__.address = address;
			<WriteAsync>d__.value = value;
			<WriteAsync>d__.<>1__state = -1;
			<WriteAsync>d__.<>t__builder.Start<FujiSPBOverTcp.<WriteAsync>d__11>(ref <WriteAsync>d__);
			return <WriteAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc />
		// Token: 0x06000C5D RID: 3165 RVA: 0x000484FC File Offset: 0x000466FC
		[DebuggerStepThrough]
		public override Task<OperateResult<bool[]>> ReadBoolAsync(string address, ushort length)
		{
			FujiSPBOverTcp.<ReadBoolAsync>d__12 <ReadBoolAsync>d__ = new FujiSPBOverTcp.<ReadBoolAsync>d__12();
			<ReadBoolAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<bool[]>>.Create();
			<ReadBoolAsync>d__.<>4__this = this;
			<ReadBoolAsync>d__.address = address;
			<ReadBoolAsync>d__.length = length;
			<ReadBoolAsync>d__.<>1__state = -1;
			<ReadBoolAsync>d__.<>t__builder.Start<FujiSPBOverTcp.<ReadBoolAsync>d__12>(ref <ReadBoolAsync>d__);
			return <ReadBoolAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc />
		// Token: 0x06000C5E RID: 3166 RVA: 0x00048550 File Offset: 0x00046750
		[DebuggerStepThrough]
		public override Task<OperateResult> WriteAsync(string address, bool value)
		{
			FujiSPBOverTcp.<WriteAsync>d__13 <WriteAsync>d__ = new FujiSPBOverTcp.<WriteAsync>d__13();
			<WriteAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<WriteAsync>d__.<>4__this = this;
			<WriteAsync>d__.address = address;
			<WriteAsync>d__.value = value;
			<WriteAsync>d__.<>1__state = -1;
			<WriteAsync>d__.<>t__builder.Start<FujiSPBOverTcp.<WriteAsync>d__13>(ref <WriteAsync>d__);
			return <WriteAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc />
		// Token: 0x06000C5F RID: 3167 RVA: 0x000485A2 File Offset: 0x000467A2
		public override string ToString()
		{
			return string.Format("FujiSPBOverTcp[{0}:{1}]", this.IpAddress, this.Port);
		}

		/// <summary>
		/// 将int数据转换成SPB可识别的标准的数据内容，例如 2转换为0200 , 200转换为0002
		/// </summary>
		/// <param name="address">等待转换的数据内容</param>
		/// <returns>转换之后的数据内容</returns>
		// Token: 0x06000C60 RID: 3168 RVA: 0x000485C0 File Offset: 0x000467C0
		public static string AnalysisIntegerAddress(int address)
		{
			string text = address.ToString("D4");
			return text.Substring(2) + text.Substring(0, 2);
		}

		/// <summary>
		/// 计算指令的和校验码
		/// </summary>
		/// <param name="data">指令</param>
		/// <returns>校验之后的信息</returns>
		// Token: 0x06000C61 RID: 3169 RVA: 0x000485F4 File Offset: 0x000467F4
		public static string CalculateAcc(string data)
		{
			byte[] bytes = Encoding.ASCII.GetBytes(data);
			int num = 0;
			for (int i = 0; i < bytes.Length; i++)
			{
				num += (int)bytes[i];
			}
			return num.ToString("X4").Substring(2);
		}

		/// <summary>
		/// 创建一条读取的指令信息，需要指定一些参数，单次读取最大105个字
		/// </summary>
		/// <param name="station">PLC的站号</param>
		/// <param name="address">地址信息</param>
		/// <param name="length">数据长度</param>
		/// <returns>是否成功的结果对象</returns>
		// Token: 0x06000C62 RID: 3170 RVA: 0x00048644 File Offset: 0x00046844
		public static OperateResult<byte[]> BuildReadCommand(byte station, string address, ushort length)
		{
			station = (byte)HslHelper.ExtractParameter(ref address, "s", (int)station);
			OperateResult<FujiSPBAddress> operateResult = FujiSPBAddress.ParseFrom(address);
			bool flag = !operateResult.IsSuccess;
			OperateResult<byte[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<byte[]>(operateResult);
			}
			else
			{
				result = FujiSPBOverTcp.BuildReadCommand(station, operateResult.Content, length);
			}
			return result;
		}

		/// <summary>
		/// 创建一条读取的指令信息，需要指定一些参数，单次读取最大105个字
		/// </summary>
		/// <param name="station">PLC的站号</param>
		/// <param name="address">地址信息</param>
		/// <param name="length">数据长度</param>
		/// <returns>是否成功的结果对象</returns>
		// Token: 0x06000C63 RID: 3171 RVA: 0x00048690 File Offset: 0x00046890
		public static OperateResult<byte[]> BuildReadCommand(byte station, FujiSPBAddress address, ushort length)
		{
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append(':');
			stringBuilder.Append(station.ToString("X2"));
			stringBuilder.Append("09");
			stringBuilder.Append("FFFF");
			stringBuilder.Append("00");
			stringBuilder.Append("00");
			stringBuilder.Append(address.GetWordAddress());
			stringBuilder.Append(FujiSPBOverTcp.AnalysisIntegerAddress((int)length));
			stringBuilder.Append("\r\n");
			return OperateResult.CreateSuccessResult<byte[]>(Encoding.ASCII.GetBytes(stringBuilder.ToString()));
		}

		/// <summary>
		/// 创建一条读取多个地址的指令信息，需要指定一些参数，单次读取最大105个字
		/// </summary>
		/// <param name="station">PLC的站号</param>
		/// <param name="address">地址信息</param>
		/// <param name="length">数据长度</param>
		/// <param name="isBool">是否位读取</param>
		/// <returns>是否成功的结果对象</returns>
		// Token: 0x06000C64 RID: 3172 RVA: 0x00048730 File Offset: 0x00046930
		public static OperateResult<byte[]> BuildReadCommand(byte station, string[] address, ushort[] length, bool isBool)
		{
			bool flag = address == null || length == null;
			OperateResult<byte[]> result;
			if (flag)
			{
				result = new OperateResult<byte[]>("Parameter address or length can't be null");
			}
			else
			{
				bool flag2 = address.Length != length.Length;
				if (flag2)
				{
					result = new OperateResult<byte[]>(StringResources.Language.TwoParametersLengthIsNotSame);
				}
				else
				{
					StringBuilder stringBuilder = new StringBuilder();
					stringBuilder.Append(':');
					stringBuilder.Append(station.ToString("X2"));
					stringBuilder.Append((6 + address.Length * 4).ToString("X2"));
					stringBuilder.Append("FFFF");
					stringBuilder.Append("00");
					stringBuilder.Append("04");
					stringBuilder.Append("00");
					stringBuilder.Append(address.Length.ToString("X2"));
					for (int i = 0; i < address.Length; i++)
					{
						station = (byte)HslHelper.ExtractParameter(ref address[i], "s", (int)station);
						OperateResult<FujiSPBAddress> operateResult = FujiSPBAddress.ParseFrom(address[i]);
						bool flag3 = !operateResult.IsSuccess;
						if (flag3)
						{
							return OperateResult.CreateFailedResult<byte[]>(operateResult);
						}
						stringBuilder.Append(operateResult.Content.TypeCode);
						stringBuilder.Append(length[i].ToString("X2"));
						stringBuilder.Append(FujiSPBOverTcp.AnalysisIntegerAddress((int)operateResult.Content.Address));
					}
					stringBuilder[1] = station.ToString("X2")[0];
					stringBuilder[2] = station.ToString("X2")[1];
					stringBuilder.Append("\r\n");
					result = OperateResult.CreateSuccessResult<byte[]>(Encoding.ASCII.GetBytes(stringBuilder.ToString()));
				}
			}
			return result;
		}

		/// <summary>
		/// 创建一条别入byte数据的指令信息，需要指定一些参数，按照字单位，单次写入最大103个字
		/// </summary>
		/// <param name="station">站号</param>
		/// <param name="address">地址</param>
		/// <param name="value">数组值</param>
		/// <returns>是否创建成功</returns>
		// Token: 0x06000C65 RID: 3173 RVA: 0x00048904 File Offset: 0x00046B04
		public static OperateResult<byte[]> BuildWriteByteCommand(byte station, string address, byte[] value)
		{
			station = (byte)HslHelper.ExtractParameter(ref address, "s", (int)station);
			OperateResult<FujiSPBAddress> operateResult = FujiSPBAddress.ParseFrom(address);
			bool flag = !operateResult.IsSuccess;
			OperateResult<byte[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<byte[]>(operateResult);
			}
			else
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.Append(':');
				stringBuilder.Append(station.ToString("X2"));
				stringBuilder.Append("00");
				stringBuilder.Append("FFFF");
				stringBuilder.Append("01");
				stringBuilder.Append("00");
				stringBuilder.Append(operateResult.Content.GetWordAddress());
				stringBuilder.Append(FujiSPBOverTcp.AnalysisIntegerAddress(value.Length / 2));
				stringBuilder.Append(value.ToHexString());
				stringBuilder[3] = ((stringBuilder.Length - 5) / 2).ToString("X2")[0];
				stringBuilder[4] = ((stringBuilder.Length - 5) / 2).ToString("X2")[1];
				stringBuilder.Append("\r\n");
				result = OperateResult.CreateSuccessResult<byte[]>(Encoding.ASCII.GetBytes(stringBuilder.ToString()));
			}
			return result;
		}

		/// <summary>
		/// 创建一条别入byte数据的指令信息，需要指定一些参数，按照字单位，单次写入最大103个字
		/// </summary>
		/// <param name="station">站号</param>
		/// <param name="address">地址</param>
		/// <param name="value">数组值</param>
		/// <returns>是否创建成功</returns>
		// Token: 0x06000C66 RID: 3174 RVA: 0x00048A38 File Offset: 0x00046C38
		public static OperateResult<byte[]> BuildWriteBoolCommand(byte station, string address, bool value)
		{
			station = (byte)HslHelper.ExtractParameter(ref address, "s", (int)station);
			OperateResult<FujiSPBAddress> operateResult = FujiSPBAddress.ParseFrom(address);
			bool flag = !operateResult.IsSuccess;
			OperateResult<byte[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<byte[]>(operateResult);
			}
			else
			{
				bool flag2 = address.StartsWith("X") || address.StartsWith("Y") || address.StartsWith("M") || address.StartsWith("L") || address.StartsWith("TC") || address.StartsWith("CC");
				if (flag2)
				{
					bool flag3 = address.IndexOf('.') < 0;
					if (flag3)
					{
						operateResult.Content.BitIndex = (int)(operateResult.Content.Address % 16);
						operateResult.Content.Address = operateResult.Content.Address / 16;
					}
				}
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.Append(':');
				stringBuilder.Append(station.ToString("X2"));
				stringBuilder.Append("00");
				stringBuilder.Append("FFFF");
				stringBuilder.Append("01");
				stringBuilder.Append("02");
				stringBuilder.Append(operateResult.Content.GetWriteBoolAddress());
				stringBuilder.Append(value ? "01" : "00");
				stringBuilder[3] = ((stringBuilder.Length - 5) / 2).ToString("X2")[0];
				stringBuilder[4] = ((stringBuilder.Length - 5) / 2).ToString("X2")[1];
				stringBuilder.Append("\r\n");
				result = OperateResult.CreateSuccessResult<byte[]>(Encoding.ASCII.GetBytes(stringBuilder.ToString()));
			}
			return result;
		}

		/// <summary>
		/// 检查反馈的数据信息，是否包含了错误码，如果没有包含，则返回成功
		/// </summary>
		/// <param name="content">原始的报文返回</param>
		/// <returns>是否成功的结果对象</returns>
		// Token: 0x06000C67 RID: 3175 RVA: 0x00048C00 File Offset: 0x00046E00
		public static OperateResult<byte[]> CheckResponseData(byte[] content)
		{
			bool flag = content[0] != 58;
			OperateResult<byte[]> result;
			if (flag)
			{
				result = new OperateResult<byte[]>((int)content[0], "Read Faild:" + SoftBasic.ByteToHexString(content, ' '));
			}
			else
			{
				string @string = Encoding.ASCII.GetString(content, 9, 2);
				bool flag2 = @string != "00";
				if (flag2)
				{
					result = new OperateResult<byte[]>(Convert.ToInt32(@string, 16), FujiSPBOverTcp.GetErrorDescriptionFromCode(@string));
				}
				else
				{
					bool flag3 = content[content.Length - 2] == 13 && content[content.Length - 1] == 10;
					if (flag3)
					{
						content = content.RemoveLast(2);
					}
					result = OperateResult.CreateSuccessResult<byte[]>(content.RemoveBegin(11));
				}
			}
			return result;
		}

		/// <summary>
		/// 根据错误码获取到真实的文本信息
		/// </summary>
		/// <param name="code">错误码</param>
		/// <returns>错误的文本描述</returns>
		// Token: 0x06000C68 RID: 3176 RVA: 0x00048CA8 File Offset: 0x00046EA8
		public static string GetErrorDescriptionFromCode(string code)
		{
			uint num = <PrivateImplementationDetails>.ComputeStringHash(code);
			if (num <= 485321326U)
			{
				if (num <= 451766088U)
				{
					if (num != 418210850U)
					{
						if (num == 451766088U)
						{
							if (code == "07")
							{
								return StringResources.Language.FujiSpbStatus07;
							}
						}
					}
					else if (code == "09")
					{
						return StringResources.Language.FujiSpbStatus09;
					}
				}
				else if (num != 468543707U)
				{
					if (num == 485321326U)
					{
						if (code == "05")
						{
							return StringResources.Language.FujiSpbStatus05;
						}
					}
				}
				else if (code == "06")
				{
					return StringResources.Language.FujiSpbStatus06;
				}
			}
			else if (num <= 518876564U)
			{
				if (num != 502098945U)
				{
					if (num == 518876564U)
					{
						if (code == "03")
						{
							return StringResources.Language.FujiSpbStatus03;
						}
					}
				}
				else if (code == "04")
				{
					return StringResources.Language.FujiSpbStatus04;
				}
			}
			else if (num != 535654183U)
			{
				if (num != 552431802U)
				{
					if (num == 1861086084U)
					{
						if (code == "0C")
						{
							return StringResources.Language.FujiSpbStatus0C;
						}
					}
				}
				else if (code == "01")
				{
					return StringResources.Language.FujiSpbStatus01;
				}
			}
			else if (code == "02")
			{
				return StringResources.Language.FujiSpbStatus02;
			}
			return StringResources.Language.UnknownError;
		}

		// Token: 0x04000361 RID: 865
		private byte station = 1;
	}
}
